import request from '../utils/request'
import { GlobalData } from '/store/state'

export const feedApi = {
  async voteTeamer(params: VoteTeamerParams = {}): Promise<{ errorcode: number, message: string, data: { user_votetotal: string, to_user_id: number } }> {
    return request.post('/vote', params)
  },
  /** 获取随机热门动态 */
  async getRandomFeeds(params: RandomFeedParams = {}): Promise<DomoFeed[] | any[]> {
    return request.post('/feeds-hot', params)
  },
  /** 批量获取赛事列表 */
  async getMatchList(params: GetMatchsParamsBase = {}): Promise<{
    data: any
  }> {
    return request.get('/events', { params })
  },
  /** 批量获取动态 */
  async getFeeds(params: GetFeedsParams = {}): Promise<DomoFeed[]> {
    return request.get('/feeds', { params })
  },

  /** 动态点赞 */
  async likeFeed(feedId: number): Promise<GetRewardMessageReturns> {
    return request.post(`/feeds/${feedId}/like`)
  },

  /** 动态取消点赞 */
  async unlikeFeed(feedId: number): Promise<void> {
    return request.delete(`/feeds/${feedId}/unlike`)
  },

  /** 获取动态详情 */
  async getFeedDetail(feedId: number): Promise<DomoFeed> {
    return request.get(`/feeds/${feedId}`)
  },

  /** 获取话题详情 */
  async getThemeDetail(themeId: number | string): Promise<DomoTheme> {
    return request.get(`/feed-themes/${themeId}`)
  },

  /** 获取话题列表 */
  async getThemeList(params: GetThemeListParams = {}): Promise<DomoTheme[]> {
    return request.get('/feed-themes', { params })
  },

  /** 获取话题下热门动态 */
  async getThemeHotFeeds(themeId: number, page: number, onlyVideo?: 1): Promise<any> {
    return request.get(`/feed-themes/${themeId}/feeds`, {
      params: {
        page,
        only_video: onlyVideo || '',
      },
    })
  },

  /** 获取话题下最新动态 */
  async getThemeNewFeeds(params: GetThemeNewFeedsPramas): Promise<DomoFeed[]> {
    return request.get('/feeds', { params })
  },
  /** 获取某动态的评论 */
  async getFeedComments(feedId: number, params: QueryParamsWithAfter = {}): Promise<DomoComments> {
    return request.get(`/feeds/${feedId}/comments`, { params })
  },

  /** 获取某评论的回复 */
  async getCommentReplys(
    commentId: number,
    params: QueryParamsWithAfter = {},
  ): Promise<{ replys: DomoComments; comment: DomoComment }> {
    return request.get(`/feeds/comments/${commentId}/replys`, { params })
  },

  /** 评论动态 */
  async postFeedComment(feedId: number, body: PostCommentBody): Promise<PostCommentReturns> {
    return request.post(`/feeds/${feedId}/comments`, body)
  },

  /** 删除动态评论 */
  async deleteFeedComment(feedId: number, commentId: number): Promise<void> {
    return request.delete(`/feeds/${feedId}/comments/${commentId}`)
  },
  /** 删除动态 */
  async deleteFeed(feedId: number): Promise<void> {
    return request.delete(`/feeds/${feedId}/currency`)
  },

  /** 发布动态 */
  async postFeed(payload: PostFeedPayload): Promise<DomoFeed> {
    const defaultPayload = {
      feed_from: 5,
      feed_mark: GlobalData.logged!.id + Date.now(),
    }
    return request.post('/feeds', { ...defaultPayload, ...payload })
  },
  /** 获取圈子总数 */
  getFeedCount() {
    return request.get('/feed/topic/count')
  },
  categiries(): Promise<DomoFeedCategory[]> {
    return request.get('/event-categories?trashed=with')
  },
  /** 创建话题 */
  CreatTheme(payload: CreatTheme) {
    return request.post('/feed-themes', payload)
  },
  /** 编辑话题 */
  editorTheme(themeId: number, payload: CreatTheme) {
    return request.put(`/feed-themes/${themeId}`, payload)
  },
  /** 删除话题 */
  deleteTheme(themeId: number) {
    return request.delete(`/feed-themes/${themeId}`)
  },
  /** 推荐动态到分类 */
  pushToCategory(feedId: number, payload: number[]) {
    return request.put(`/feeds/${feedId}/pushed-categories`, payload)
  },
  /** 取消推荐 */
  popToCategory(feedId: number, categoryId: number) {
    return request.delete(`/feed/categories/${categoryId}/feeds/${feedId}`)
  },
  async getNavbarList(): Promise<{
    data: any
  }> {
    return request.get('/manage_menu/menu_list?is_hidden=1')
  },
}
